/**
 * Created by Administrator on 2020/5/18.
 */
;
var helper = require("../../javascript/helper");
var db = require("../../javascript/mysql");
var request = require("request");
var mysql = new db.mysqlClient("accident");
var url = "https://www.ebgic.com/nvhl/countPrice.json";
function genProductInfo() {
    var projects = [];
    plan = {
        "planA": {
            //年龄段
            "year": ["1-70"],
            //保障天数
            "days": ["7d", "15d", "30d", "50d"],
            //保障人数
            "people_count": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            //境外意外伤残
            "accident_death": ["100000"],
            //境外医疗保险
            "accident_medical": ["10000"],
            //急性病身故
            "acute_death": ["50000"],
            //居家财产险
            "family_property": ["20000"]

        },
        "planB": {
            //年龄段
            "year": ["1-70"],
            //保障天数
            "days": ["7d", "15d", "30d", "50d"],
            //保障人数
            "people_count": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            //境外意外伤残
            "accident_death": ["200000"],
            //境外医疗保险
            "accident_medical": ["20000"],
            //急性病身故
            "acute_death": ["100000"],
            //居家财产险
            "family_property": ["40000"]

        },
        "planC": {
            //年龄段
            "year": ["1-70"],
            //保障天数
            "days": ["7d", "15d", "30d", "50d"],
            //保障人数
            "people_count": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            //境外意外伤残
            "accident_death": ["300000"],
            //境外医疗保险
            "accident_medical": ["30000"],
            //急性病身故
            "acute_death": ["150000"],
            //居家财产险
            "family_property": ["60000"]

        },
        "planD": {
            //年龄段
            "year": ["1-70"],
            //保障天数
            "days": ["7d", "15d", "30d", "50d"],
            //保障人数
            "people_count": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            //境外意外伤残
            "accident_death": ["500000"],
            //境外医疗保险
            "accident_medical": ["50000"],
            //急性病身故
            "acute_death": ["250000"],
            //居家财产险
            "family_property": ["100000"]
        }

    };

    for (k in plan) {
        data = helper.product([plan[k]["year"], plan[k]["days"], plan[k]["people_count"], plan[k]["accident_death"], plan[k]["accident_medical"], plan[k]["acute_death"], plan[k]["family_property"]])
        for (i = 0; i < data.length; i++) {
            dt = helper.formatProductInfo(data[i], plan[k]);
            projects.push( dt );

        }
    }
    return projects;

}
//执行爬虫
function httpRequest(dt) {

    if (dt["accident_death"] == "100000") {

        params = {
            "insuranceid_25419_保险金额": "100000",
            "insuranceid_25420_保险金额": "10000",
            "insuranceid_25421_保险金额": "50000",
            "insuranceid_25422_保险金额": "20000",
            "suiteId": "5761",
            "period": dt.days.replace("d", ""),
            "amount": "",
            "personCount": dt.people_count,
            "backInsuranceCount": "",
            "trafficAccident": "abroadTravel"
        };


    } else if (dt["accident_death"] == "200000") {

        params = {
            "insuranceid_25423_保险金额": "200000",
            "insuranceid_25424_保险金额": "20000",
            "insuranceid_25425_保险金额": "100000",
            "insuranceid_25426_保险金额": "40000",
            "suiteId": "5762",
            "period": dt.days.replace("d", ""),
            "amount": "",
            "personCount": dt.people_count,
            "backInsuranceCount": "",
            "trafficAccident": "abroadTravel"
        };


    } else if (dt["accident_death"] == "300000") {

        params = {
            "insuranceid_25427_保险金额": "300000",
            "insuranceid_25428_保险金额": "30000",
            "insuranceid_25429_保险金额": "150000",
            "insuranceid_25430_保险金额": "60000",
            "suiteId": "5763",
            "period": dt.days.replace("d", ""),
            "amount": "",
            "personCount": dt.people_count,
            "backInsuranceCount": "",
            "trafficAccident": "abroadTravel"
        };


    } else {

        params = {
            "insuranceid_25431_保险金额": "500000",
            "insuranceid_25432_保险金额": "50000",
            "insuranceid_25433_保险金额": "250000",
            "insuranceid_25434_保险金额": "100000",
            "suiteId": "5764",
            "period": dt.days.replace("d", ""),
            "amount": "",
            "personCount": dt.people_count,
            "backInsuranceCount": "",
            "trafficAccident": "abroadTravel"
        };


    }
    payload = helper.urlencode(params);
    return new Promise(function (resolve, reject) {
        request({
            url: url,
            method: "POST",
            json: false,
            headers: {

                "Accept-Language": "zh-CN,zh;q=0.9",
                "Connection": "keep-alive",
                "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                "Cookie": "MEIQIA_TRACK_ID=1aFJ6Ncn7kUoAV8VJ7u8Tftm9x0; JSESSIONID1=wcQmmZ7OHrGSGsWsaJPQaAWDOER4O4A9AuLaqUfEZudr7Iz6w2ij!-1524261001; MEIQIA_VISIT_ID=1c4QiEh3uqdcSKJkpQ8UqS0RElW",
                "Sec-Fetch-Dest": "empty",
                "Sec-Fetch-Mode": "cors",
                "Sec-Fetch-Site": "same-origin",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36",
                "X-Requested-With": "XMLHttpRequest"
            },
            body: payload
        }, function (err, res, body) {

            if (!err && res.statusCode == 200) {

                if (body.indexOf("totalPrice") != -1) {
                    price = (JSON.parse(body).totalPrice.toFixed(2)).toString();

                    rs = {
                        "name": "境外旅游保险",
                        "company": "北部湾财产保险",
                        "price": price
                    };
                    resolve( rs );

                } else {
                    console.log("更新数据信息有误!!!!")
                }
            }

        })

    })
}
var main = async function () {
    data = genProductInfo();
    for(i=0;i<data.length;i++){
        dt = data[i];
        var body = await httpRequest(dt);
        result = Object.assign(dt,body);
        mysql.insertData( result );
    }
    mysql.close();

}
